package com.itfreer.print.base.itextpdf.textbefore;

import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itfreer.print.base.itextpdf.ITextBefore;

/**
 * 图片水印
 */
public class PictureWaterMarkBefore implements ITextBefore {

	private String waterMarkFullFilePath;
	
	public PictureWaterMarkBefore(String waterMarkFullFilePath){
		this.waterMarkFullFilePath = waterMarkFullFilePath;
	}
	
	/**
	 * 文档操作
	 * 
	 * @param pdfDocument
	 * @param pdfWriter
	 */
	public void doOptions(Document pdfDocument, PdfWriter pdfWriter) {
		if (pdfWriter == null) {
			return;
		}
		// 通过PDF页面事件模式添加图片水印功能
		pdfWriter.setPageEvent(new PictureWaterMarkPdfPageEvent(waterMarkFullFilePath));
	}
	
	/**
	 * 变更一个图片对象的展示位置和角度信息
	 * 
	 * @param waterMarkImage
	 * @param xPosition
	 * @param yPosition
	 * @return
	 */
	private static Image getWaterMarkImage(Image waterMarkImage, float xPosition, float yPosition) {
		waterMarkImage.setAbsolutePosition(xPosition, yPosition);// 坐标
		waterMarkImage.setRotation(-20);// 旋转 弧度
		waterMarkImage.setRotationDegrees(-45);// 旋转 角度
		waterMarkImage.scalePercent(100);// 依照比例缩放
		return waterMarkImage;
	}

	/**
	 * 为PDF分页时创建添加图片水印的事件信息
	 */
	private class PictureWaterMarkPdfPageEvent extends PdfPageEventHelper {

		private String waterMarkFullFilePath;
		private Image waterMarkImage;

		public PictureWaterMarkPdfPageEvent(String waterMarkFullFilePath) {
			this.waterMarkFullFilePath = waterMarkFullFilePath;
		}

		public void onEndPage(PdfWriter writer, Document document) {
			try {
				float pageWidth = document.right() + document.left();// 获取pdf内容正文页面宽度
				float pageHeight = document.top() + document.bottom();// 获取pdf内容正文页面高度
				PdfContentByte waterMarkPdfContent = writer.getDirectContentUnder();
				// 仅设置一个图片实例对象，整个PDF文档只应用一个图片对象，极大减少因为增加图片水印导致PDF文档大小增加
				if (waterMarkImage == null) {
					waterMarkImage = Image.getInstance(waterMarkFullFilePath);
				}
				// 添加水印图片，文档正文内容采用横向三列，竖向两列模式增加图片水印
				waterMarkPdfContent.addImage(getWaterMarkImage(waterMarkImage, pageWidth * 0.2f, pageHeight * 0.1f));
				waterMarkPdfContent.addImage(getWaterMarkImage(waterMarkImage, pageWidth * 0.2f, pageHeight * 0.4f));
				waterMarkPdfContent.addImage(getWaterMarkImage(waterMarkImage, pageWidth * 0.2f, pageHeight * 0.7f));
				waterMarkPdfContent.addImage(getWaterMarkImage(waterMarkImage, pageWidth * 0.6f, pageHeight * 0.1f));
				waterMarkPdfContent.addImage(getWaterMarkImage(waterMarkImage, pageWidth * 0.6f, pageHeight * 0.4f));
				waterMarkPdfContent.addImage(getWaterMarkImage(waterMarkImage, pageWidth * 0.6f, pageHeight * 0.7f));
				
				PdfGState gs = new PdfGState();
				gs.setFillOpacity(0.2f);// 设置透明度为0.2
				waterMarkPdfContent.setGState(gs);
			} catch (DocumentException de) {
				de.printStackTrace();
				System.err.println("pdf watermark font: " + de.getMessage());
			} catch (IOException de) {
				de.printStackTrace();
				System.err.println("pdf watermark font: " + de.getMessage());
			}
		}
	}
}
